<template>
  <PageLayout class="form-container">
    <div class="form-title">{{ route.query.id ? "编辑" : "新增" }}套餐管理（多次卡）</div>
    <div class="form-box w-800">
      <a-form :model="formState" :rules="rules" ref="formRef" layout="Vertical" :labelCol="{ style: { width: '130px' } }">
        <!-- <div class="cont-title">基本信息</div> -->
        <a-form-item label="套餐名称" name="name" required>
          <a-input v-model:value="formState.name" placeholder="请输入套餐名称" :maxlength="50" />
        </a-form-item>
        <a-form-item label="套餐描述" name="content" required>
          <a-input v-model:value="formState.content" placeholder="请输入套餐描述" :maxlength="50" />
        </a-form-item>
        <a-form-item label="绑定套餐" name="packageId" required>
          <a-select v-model:value="formState.packageId" placeholder="请选择绑定套餐" :options="packageOptions" />
        </a-form-item>
        <a-form-item label="金额" name="payFee" required>
          <a-input v-model:value="formState.payFee" placeholder="请输入金额" :maxlength="50" />
        </a-form-item>
        <a-form-item label="使用次数" name="totalNum" required>
          <a-input v-model:value="formState.totalNum" placeholder="请输入使用次数" :maxlength="50" />
        </a-form-item>

        <a-col :span="24">
          <a-form-item class="btn-box" style="margin-left: 80px">
            <a-button @click="router.go(-1)">返回</a-button>
            <a-button type="primary" @click="save">保存</a-button>
          </a-form-item>
        </a-col>
      </a-form>
    </div>
  </PageLayout>
</template>

<script setup>
import {
  postSinglePackageList,
  postPackageMultipleAdd,
  postPackageMultipleUpdate,
  //   postMenuTreeList,
  //   postUserDetail,
} from "@/api/common";
import { useStoreCommonEnum } from "@/store";
import { message } from "ant-design-vue";
import { onMounted } from "vue";
const storeCommonEnum = useStoreCommonEnum();

const route = useRoute();
const router = useRouter();
const formRef = ref(null);

const formState = reactive({
  id: "",
  name: "",
  content: "",
  username: "",
  packageId: undefined,
  payFee: undefined,
  totalNum: undefined,

});
const areaOptions = ref([
  { label: "俄语区", value: "1" },
  { label: "非洲区", value: "2" },
  { label: "亚太区", value: "3" },
  { label: "欧洲区", value: "4" },
  { label: "中东区", value: "5" },
  { label: "拉美区", value: "6" },
]);
const treeData = ref([]);
const packageOptions = ref([]);
const jobOptions = ref([]);

onMounted(async () => {
  postSinglePackageList({

  }).then(res => {
    packageOptions.value = res.data?.map(item => {
      return {
        label: item.name,
        value: item.id,
      }
    })
  })
  // jobOptions.value = await storeCommonEnum.getCommonEnum("job");
  //   postMenuTreeList().then((res) => {
  //     treeData.value = res.data;
  //   });
  //   if (route.query.id) {
  //     postUserDetail({ id: route.query.id }).then((res) => {
  //       for (let key in formState) {
  //         formState[key] = res.data[key];
  //       }
  //     });
  //   }
});

const rules = {
  nickname: [{ required: true, message: "请输入套餐名称", trigger: "blur" }],
  username: [{ required: true, message: "请输入套餐描述", trigger: "blur" }],
  packageId: [{ required: true, message: "请选择绑定套餐", trigger: "blur" }],
  regionCodes: [{ required: true, message: "请选择国际事业部人员负责区域" }],
};

const save = () => {
  formRef.value.validate().then(() => {
    // const requestUrl = formState.id ? postPackageMultipleUpdate : postPackageMultipleAdd;
    postPackageMultipleAdd({ ...formState }).then(() => {
      message.success(formState.id ? "编辑成功" : "新增成功");
      router.go(-1);
    });
  });
};
</script>

<style lang="less" scoped>
.btn-box {
  margin-top: 24px;
  margin-left: 130px;
  :deep(.ant-btn) {
    margin-right: 16px;
  }
}
</style>
